package file;

/**
 * Author: mb25428
 * Create Date:  31/3/2013
 * 
 * Comments: this java class is for the image processing,including edge enhance, sharpen, blurs, anaglyph and edge detect
 * JDK version: <JDK 1.7>
 * 
 * Modified By: mb25428
 * Modified Date: 4/3/2013
 * Why&What is Modified: Add the Gray Image Processing
 * 
 */




import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;


public class ImageProcess {

	static float[][]data={
				
				//edgeEnhanc
				{0f,-1f,0f,-1f,5f,-1f,0f,-1f,0f},
				
				
				//sharpen
				{	  -0.125f,-0.125f,-0.125f,
				      -0.125f,2f,-0.125f,
				      -0.125f,-0.125f,-0.125f},
				//blurs	
			     {0.09375f,0.09375f,0.09375f,
			      0.09375f,0.25f,0.09375f,
			      0.09375f,0.09375f,0.09375f},
			      
			     //anaglyph
			     {2f,0f,2f,
			      0f,0f,0f,
			      2f,0f,-5f},
			      
			      //edge detect
			      {0.0f,-1.0f,0.0f,-1.0f,4.f,-1.0f,0.0f,-1.0f,0.0f}
		};
		
	
		/**
		 * 
		 * @param sourceBuf
		 * @return Gray image
		 */
		public static BufferedImage processGrayImage(BufferedImage sourceBuf){
			
			int width=sourceBuf.getWidth();
			int height=sourceBuf.getHeight();

			
			BufferedImage targetBuf=new BufferedImage(
				                        width,height,BufferedImage.TYPE_3BYTE_BGR);
			
			ColorConvertOp cco=new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),null);
			
			//do the gray operation
			cco.filter(sourceBuf,targetBuf);      
			return  targetBuf;
		}
		
		/**
		 * 
		 * @param sourceBuf
		 * @return RGB image
		 */
		public static BufferedImage processToRGB(BufferedImage sourceBuf){
			//get the width and height of the image
			int width=sourceBuf.getWidth();
			int height=sourceBuf.getHeight();

			//create the target image which save the processed iamge
			BufferedImage targetBuf=new BufferedImage(
				                        width,height,BufferedImage.TYPE_3BYTE_BGR);
			
			//创建用来进行灰度处理的ColorConvertOp对象
			ColorConvertOp cco=new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_sRGB),null);
			//进行灰度处理
			cco.filter(sourceBuf,targetBuf);      
			//返回处理后图像对应的图标对象
			return  targetBuf;
		}
		
		/**
		 * 
		 * @param sourceBuf
		 * @return the edge enhance image
		 */
		public static BufferedImage edgeEnhance(BufferedImage sourceBuf){
			int width=sourceBuf.getWidth();
			int height=sourceBuf.getHeight();
			
			BufferedImage sourceBuf1 = processToRGB(sourceBuf);
			
	         BufferedImage  bimg=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); 
	         Kernel kernel=new Kernel(3,3,data[0]); 
	         
	         ConvolveOp cop=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
	         
	         cop.filter(sourceBuf1,bimg);
	         return bimg;
		}
		
		/**
		 * @param sourceBuf
		 * @return the sharpen image
		 * 
		 */
		public static BufferedImage sharpen(BufferedImage sourceBuf){
			int width=sourceBuf.getWidth();
			int height=sourceBuf.getHeight();
			
	         BufferedImage  bimg=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); 
	         Kernel kernel=new Kernel(3,3,data[1]); 
	         
	         ConvolveOp cop=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
	         
	         cop.filter(sourceBuf,bimg);
	         return bimg;
		}
		/**
		 * 
		 * @param sourceBuf
		 * @return the blurs image
		 */
		public static BufferedImage blurs(BufferedImage sourceBuf){
			int width=sourceBuf.getWidth();
			int height=sourceBuf.getHeight();
			
	         BufferedImage  bimg=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); 
	         Kernel kernel=new Kernel(3,3,data[2]); 
	         
	         ConvolveOp cop=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
	         
	         cop.filter(sourceBuf,bimg);
	         return bimg;
		}
		
		public static BufferedImage anaglyph(BufferedImage sourceBuf){
			int width=sourceBuf.getWidth();
			int height=sourceBuf.getHeight();
			
	         BufferedImage  bimg=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); 
	         Kernel kernel=new Kernel(3,3,data[3]); 
	         
	         ConvolveOp cop=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
	         
	         cop.filter(sourceBuf,bimg);
	         return bimg;
		}
		
		/**
		 * 
		 * @param sourceBuf
		 * @return BufferedImage 
		 */
		
		public static BufferedImage edgeDetect(BufferedImage sourceBuf){
			
			int width=sourceBuf.getWidth();
			int height=sourceBuf.getHeight();
			
	         BufferedImage  bimg=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); 
	         Kernel kernel=new Kernel(3,3,data[4]); 
	         
	         ConvolveOp cop=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
	         
	         cop.filter(sourceBuf,bimg);
	         return bimg;
		}
		
		/**
		 * 
		 * @param sourceBuf
		 * @return
		 * not finished
		 */
		public static BufferedImage smoothImage(BufferedImage sourceBuf){
			int width=sourceBuf.getWidth();
			int height=sourceBuf.getHeight();
			
			 //bi的存在是为了要把img转换成BufferedImage.这招瞒有用     
	         //BufferedImage bi= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 

	         //存卷积后的BufferedImage(处理后的图像)
	         BufferedImage  bimg=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); 
	         Kernel kernel=new Kernel(3,3,data[1]); 
	         //ConvolveOp 专门用于图像的卷积
	         ConvolveOp cop=new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
	         cop.filter(sourceBuf,bimg); //过滤后生成新的BufferedImage 
	         return bimg;
		}
}
